<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<?php 
	if( !isset($_COOKIE["username"]) ){
		echo "<script>window.location.href='login.php'</script>";
	}else{
		setcookie("username", $_COOKIE["username"],time() + 900);
	}
?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>SMS Message</title>
<link href="css/template.css" rel="stylesheet" type="text/css" />
<link href="css/test/SMSMessage.css" rel="stylesheet" type="text/css" />
<script src="js/droplinemenu.js" type="text/javascript"></script>
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript">
//build menu with DIV ID="myslidemenu" on page:
droplinemenu.buildmenu("nav")
</script>

<link href="css/ui-lightness/jquery-ui-1.9.2.custom.css" rel="stylesheet">
<script src="js/jquery-1.8.3.js"></script>
<script src="js/jquery-ui-1.9.2.custom.js"></script>



<script>


	jQuery(document).ready(function(){
		jQuery("#ViewMobileListDialog").dialog({
			autoOpen:false,				
			modal:true,
			width:900,
			buttons:[
			         {
				         text:"add",
				         click:function(){
					        	var isSelectedList = document.getElementsByName("mobileSelect");
					     		var selectData = "";
					     		var isFirst = true;
					     		for( var i = 0; i < isSelectedList.length; i++ ){
					     			if( isSelectedList[i].checked == true ){
						     			if( isFirst == true ){
					     					selectData += isSelectedList[i].value;
					     					isFirst = false;
						     			}else{
							     			selectData += "," + isSelectedList[i].value;
						     			}
					     			}
					     		}
					     		if( document.getElementById("EsendexRecipient").value == "" ){
					     			document.getElementById("EsendexRecipient").innerHTML = document.getElementById("EsendexRecipient").innerHTML + selectData;
					     		}else{
					     			document.getElementById("EsendexRecipient").innerHTML =  document.getElementById("EsendexRecipient").innerHTML + "," + selectData;
					     		}
					     		$(this).dialog("close");
					         }
			         
				         
			         },
			         {
				         text:"cancel",
				         click:function(){
					         $(this).dialog("close");
				         }
				      
				     }	
			]


			
			
		});

		jQuery("#sendResult").dialog({
			autoOpen:false,
			modal:true,
			width:400,
			buttons:[{
						text:"OK",
						click:function(){
							jQuery("#sendResult").dialog("close");
						}
					}
					]
		});

		document.getElementById("hidden_frame").onload = resultLoad;
	});


	function allSelect(objectId){
		var selectObject = document.getElementById(objectId);
		if( selectObject.value == "All Select" ){
			var isSelectedList = document.getElementsByName("mobileSelect");
			var selectData;
			for( var i = 0; i < isSelectedList.length; i++ ){
				isSelectedList[i].checked = true;	
			}
			selectObject.value = "No Select";
		}else{
			var isSelectedList = document.getElementsByName("mobileSelect");
			var selectData;
			for( var i = 0; i < isSelectedList.length; i++ ){
				isSelectedList[i].checked = false;	
			}
			selectObject.value = "All Select";
		}
	}

	function searchMobile(){
		var idText = document.getElementById("idText").value;
		var firstNameText = document.getElementById("firstNameText").value;
		var surnameText = document.getElementById("surnameText").value;
		var telephoneText = document.getElementById("telephoneText").value;
		
		
		var postData = "id=" + idText + "&firstName=" + firstNameText + "&surname=" + surnameText + "&telephone=" + telephoneText;
		jQuery.ajax({type:'post', async:false,dataType:'json', url:'get_mobile.php', data:postData, success:function(result){
				var divObject = document.getElementById("mobileList");
				var childList = divObject.childNodes;
				for( var i = childList.length - 1; i >= 0; i-- ){
					divObject.removeChild(childList[i]);
				}
	
				var tableNode = document.createElement("table");
				tableNode.setAttribute("width", "450px");
				for( var i = 0; i < result.data.length; i++ ){
					var childTr = document.createElement("tr");
	
					var NoTd = document.createElement("td");
					NoTd.innerHTML = result.data[i].No;
					NoTd.setAttribute("width","20%");
					
					var firstNameTd = document.createElement("td");
					firstNameTd.innerHTML = result.data[i].firstName;
					firstNameTd.setAttribute("width","20%");
	
					
					var surnameTd = document.createElement("td");
					surnameTd.innerHTML = result.data[i].surname;
					surnameTd.setAttribute("width","20%");
					
					var typeTd = document.createElement("td");
					typeTd.innerHTML = result.data[i].type;
					typeTd.setAttribute("width","20%");

					var mobileTd = document.createElement("td");
					var mobile = "";
					if( result.data[i].mobile.length > 35 ){
						mobile = result.data[i].mobile.substr(0,34);
					}else{
						mobile = result.data[i].mobile;
					}
					mobileTd.innerHTML = mobile;
					mobileTd.setAttribute("width", "20%");
					
					var isSelectCheck = document.createElement("input");
					isSelectCheck.type = "checkbox";
					isSelectCheck.name = "mobileSelect";
					isSelectCheck.value = result.data[i].mobile;
	
					var isSelectTd = document.createElement("td");
					isSelectTd.appendChild(isSelectCheck);
					isSelectTd.setAttribute("width","20%");
					//alert("dfasdf");
					childTr.appendChild(NoTd);
					childTr.appendChild(firstNameTd);
					childTr.appendChild(surnameTd);
					childTr.appendChild(typeTd);
					childTr.appendChild(mobileTd);
					childTr.appendChild(isSelectTd);
	
					divObject.appendChild(childTr);
	
				}
	
	
				
	
				
			},
			error:function (XMLHttpRequest, textStatus, errorThrown){
				alert(XMLHttpRequest.responseText);
				alert(textStatus);
				alert(errorThrown);
			}
		});
	}

	function popAddMobile(){
		jQuery("#ViewMobileListDialog").dialog("open");
	}


	function previousPage(){
		var lastURL = document.referrer;

		if( lastURL.indexOf("patient_account.php") != -1 ){
			window.location.href = lastURL;
			return;
		}
		
		window.location.href = "index.php";

		
	}

	function isMaxLen(object){
		var nMaxLen = object.getAttribute?parseInt(object.getAttribute("maxlength")):"";
		if( object.getAttribute && object.value.length > nMaxLen ){
			object.value = object.value.substring(0, nMaxLen);
		}
	}


	function sendMessage(){

		var telephoneErrorObject = document.getElementById("telephoneError");

		telephoneErrorObject.innerHTML = "";
		
		var EsendexUsername = document.getElementById("EsendexUsername").value;
		var EsendexPassword = document.getElementById("EsendexPassword").value;
		var EsendexAccount = document.getElementById("EsendexAccount").value;
		var EsendexRecipient = jQuery("#EsendexRecipient").val();
		var EsendexBody = document.getElementById("EsendexBody").innerText;

		var telephoneList = EsendexRecipient.split(",");
		var reg = new RegExp("^[0-9]+$");
		for( var i = 0; i < telephoneList.length; i++ ){
			if( reg.test(telephoneList[i]) == false ){
				telephoneErrorObject.innerHTML = "telephone format error!!!!";
				return false;
			}
		}
		return true;
		/*var postData = "EsendexUsername=" + EsendexUsername + "&EsendexPassword=" + EsendexPassword + "&EsendexAccount=" + EsendexAccount + "&EsendexRecipient=" + EsendexRecipient + "&EsendexBody=" + EsendexBody + "&EsendexPlainText=true&EsendexTest=true";
		jQuery.ajax({type:'post', async:true, dataType:'xml', data:postData, url:'http://www.baidu.com', success:function(result){
				alert("12345");
				alert(result);	
			},
			error:function(XMLHttpRequest, textStatus, errorThrown){
				alert(XMLHttpRequest.responseText);
				alert(textStatus);
				alert(errorThrown);
				alert(XMLHttpRequest.status);
				alert(XMLHttpRequest.readyState);
			}
		});*/

		

		
		
		
	}

	var isFirst = true;
	function resultLoad(){
		var isChrome = (window.navigator.userAgent.indexOf("Chrome") != -1);
		if( isChrome == true ){
			jQuery("#sendResult").dialog("open");
		}else{
			if( isFirst == true ){
				isFirst = false;
			}else{
				jQuery("#sendResult").dialog("open");
			}
		}
	}

	

</script>
</head>

<body>
	<div id="container">
		<div id="header"></div>
		<?php include_once 'nav.php';?>
		<?php
		 
			$ini_array = parse_ini_file("config/config.ini");
			//echo $ini_array["host"];
			$conn = @mysqli_connect($ini_array["host"], $ini_array["username"], $ini_array["password"], $ini_array["db_name"]) or die("cann't connect the mysql db");
			mysqli_set_charset($conn, "utf8");
			ini_set('default_charset', "UTF-8");
			$isMessageTest = $ini_array["isSMSMessageTest"];
				
			$patientNo = 0;
			if( isset($_GET["patientNo"])){
				$patientNo = mysqli_real_escape_string($conn,$_GET["patientNo"]);
			}
			
			$getTelephoneSql = "select mobile from patient where Patient_no=".$patientNo;
			$mobileResult = mysqli_query($conn, $getTelephoneSql);
			$telephone = "";
			if( $row = mysqli_fetch_array($mobileResult) ){
				$telephone = $row["mobile"];
			}
			
			if( isset($_GET["telephone"]) && strcmp($_GET["telephone"], "") != 0 ){
				$getTelephone = $_GET["telephone"];
				if( strcmp($telephone,"") == 0 ){
					
					$telephone .= $getTelephone;
				}else{
					$telephone .= ",";
					$telephone .= $getTelephone;
				}

			}
			
		?>
		<div id="main_content" style="text-align:center;margin-top:200px;">
			<form method="post" action="https://www.esendex.com/secure/messenger/formpost/SendSMS.aspx" target="hidden_frame" onsubmit="return sendMessage()">
				<input type="hidden" value="<?php echo $ini_array["SMSusername"]?>" name="EsendexUsername" id="EsendexUsername" />
				<input type="hidden" value="<?php echo $ini_array["SMSpassword"]?>" name="EsendexPassword" id="EsendexPassword" />
				<input type="hidden" value="<?php echo $ini_array["SMSaccount"]?>" name="EsendexAccount" id="EsendexAccount" />
				
				<div id="visualPartDiv" style="margin:10px auto;width:800px;">
					<table style="width:780px">
						<tr>
							<td style="width:33%;text-align:right;"><label>Mobile:</label></td>
							<td style="width:33%;"><textarea name="EsendexRecipient" id="EsendexRecipient" cols="40" rows="2" style="resize:none;"><?php echo $telephone?></textarea></td>
							<td style="width:33%;"><label id="telephoneError"></label></td>
						</tr>
						<tr>
							<td style="width:33%;text-align:right;vertical-align:top;"><label>Message:</label></td>
							<td style="width:33%;"><textarea name="EsendexBody" id="EsendexBody" cols="40" rows="10"  maxlength="160" onkeyup="return isMaxLen(this)" style="resize:none;"></textarea></td>
							<td style="width:33%;text-align:left;vertical-align:top;"><input type="button" value="Add Mobiles" onclick="popAddMobile()"/></td>
						</tr>
						<tr>
							<td style="width:33%;text-align:right;"></td>
							<td style="width:33%;text-align:left;"><P><INPUT type="checkbox" name="EsendexTest" <?php if(strcmp($isMessageTest,"true") == 0 ){echo "checked=\"checked\"";}?>/> Test mode</P></td>
							<td style="width:33%;"></td>
						</tr>
						<tr>
							<td></td>
							<td style="text-align:left;"><input type="submit" value="send" onclick=""/><input type="button" value="return" onclick="previousPage()"/></td>
							<td></td>
						</tr>
						
					</table>
				</div>
	
	 			
				
				
				<div style="display:none">
					
					<p><input type="checkbox" name="EsendexPlainText" checked="checked"/>Plain Text</p>
				</div>
				
				<div id="operateDiv" >
					
				</div>
				
			</form>
			<form target="hidden_frame" method="post">
				<input type="hidden" value="getInfo" />
			</form>
			
		</div>
		<?php include_once 'footer.php';?>
		
		<div id="ViewMobileListDialog" title="Add Mobile Number" style="display:none">
			<div id="mobileSearchPanel" >
				<label>NO.:</label><input type="text" id="idText" name="idText" style="width:110px;"/>
				<label>First Name:</label><input type="text" id="firstNameText" name="firstNameText" style="width:110px;"/>
				<label>Surname:</label><input type="text" id="surnameText" name="surnameText" style="width:110px;"/>
				<label>Telephone:</label><input type="text" id="telephoneText" name="telephoneText" style="width:110px;"/>
				<input type="button" value="query" onclick="searchMobile()" />
			</div>
			<div style="width:140px;display:inline-block"><label >No.</label></div>
			<div style="width:140px;display:inline-block"><label >First Name</label></div>
			<div style="width:155px;display:inline-block"><label >Surname</label></div>
			<div style="width:128px;display:inline-block"><label >Type</label></div>
			<div style="width:205px;display:inline-block"><label >mobile</label></div>
			<div style="width:60px;display:inline-block"><label >Select</label></div>
			
			<div id="mobileList" style="overflow:scroll;height:300px">
			
			</div>
			
			<input type="button" value="All Select" onclick="allSelect(this.id)" id="allSelect"/>
			
		</div>
		<div id="sendResult" title="Send Message Result">
			<iframe id="hidden_frame" name="hidden_frame"></iframe>
		</div>
	</div>
</body>
</html>
